package com.qiangshi.common.utils;

import com.github.pagehelper.PageHelper;
import com.qiangshi.common.core.page.PageDomain;
import com.qiangshi.common.core.page.TableDataInfo;
import com.qiangshi.common.core.page.TableSupport;
import com.qiangshi.common.utils.sql.SqlUtil;

import java.util.List;
import java.util.Objects;

/**
 * 分页工具类
 *
 * @author ruoyi
 */
public class PageUtils extends PageHelper {
    /**
     * 设置请求分页数据
     */
    public static void startPage() {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        if (StringUtils.isNotNull(pageNum) && StringUtils.isNotNull(pageSize)) {
            String orderBy = SqlUtil.escapeOrderBySql(pageDomain.getOrderBy());
            Boolean reasonable = pageDomain.getReasonable();
            PageHelper.startPage(pageNum, pageSize, orderBy).setReasonable(reasonable);
        }
    }
    public static TableDataInfo buildTableDataInfo(Integer page, Integer size, List<?> list) {
        if (Objects.isNull(list)) {
            return null;
        }
        TableDataInfo tableDataInfo = new TableDataInfo();
        PageInfoConvent pageInfoConvent = new PageInfoConvent(page, size, list);
        tableDataInfo.setRows(pageInfoConvent.getList());
        tableDataInfo.setTotal(list.size());
        return tableDataInfo;
    }
}
